package com.cbs.android.function.input;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cbs.android.component.application.activity.CBSDialogFragment;
import com.cbs.android.component.application.activity.delayedtask.DelayedTask;
import com.cbs.android.widget.nospacetextview.NoSpaceTextView;

/**
 * Created by zhuyj on 2015/1/20.
 * Modified by brochexu on 3/24/2015
 */

/**
 * 信息输入弹出框
 */
public class InputDialogFragment extends CBSDialogFragment {
	private EditText inputView;
	private String title;
	private OnConfirmListener onConfirmListener;
	private String content;
	private String desc;
	private int layout = R.layout.cbs_input_dialog_input;

	private int lines = 1;

	public InputDialogFragment setTitle(String title) {
		this.title = title;
		return this;
	}

	public InputDialogFragment setOnConfirmListener(OnConfirmListener onConfirmListener) {
		this.onConfirmListener = onConfirmListener;
		return this;
	}

	public InputDialogFragment setContent(String content) {
		this.content = content;
		return this;
	}

	public InputDialogFragment setLayout(int layout) {
		this.layout = layout;
		return this;
	}

	public InputDialogFragment setDesc(String desc) {
		this.desc = desc;
		return this;
	}

	public InputDialogFragment setLines(int lines) {
		this.lines = lines;
		return this;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(layout, container, false);

		// TODO need to check all code below
		// TODO layout
		TextView titleView = (TextView) view.findViewById(R.id.cbs_input_dialog_input_title);

		titleView.setText(title);
		inputView = (EditText) view.findViewById(R.id.cbs_input_dialog_input_input);
		if (lines > 1) {
			inputView.setMaxLines(lines);
		} else {
			inputView.setSingleLine(true);
		}
		if (content != null && !content.equals("null")) {
			inputView.setText(content);
		}
		if (desc != null && !desc.equals("")) {
			NoSpaceTextView textView = (NoSpaceTextView) view.findViewById(R.id.cbs_input_dialog_input_desc);
			textView.setText(desc);
			textView.setVisibility(View.VISIBLE);
		}
		Button cancelView = (Button) view.findViewById(R.id.cbs_input_dialog_input_cancel);
		Button submitView = (Button) view.findViewById(R.id.cbs_input_dialog_input_submit);
		cancelView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onCancelListener.onClick(InputDialogFragment.this);
			}
		});
		submitView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onConfirmListener.onConfirm(InputDialogFragment.this, inputView.getText().toString());
			}
		});

		doDelayed(new DelayedTask() {
			@Override
			public void doDelayed(Bundle data) {
				inputMethodManager.showSoftInput(inputView, 0);
			}
		}, 50);
		return view;
	}


	public interface OnConfirmListener {
		void onConfirm(CBSDialogFragment dialogFragment, String str);
	}
}
